package com.ducquoc.examreviewdegreea1;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class DocArrayAdapter extends ArrayAdapter<DocPojo> {
	Activity context = null;
	int layoutId;
	ArrayList<DocPojo> arrQuestion = null;
	int flag = 0;

	// Contructor nay dung de lay nhung gia tri duoc truyen vao tu DocActivity
	public DocArrayAdapter(Activity context, int layoutId,
			ArrayList<DocPojo> list, int flag) {
		super(context, layoutId, list);

		this.context = context;
		this.layoutId = layoutId;
		this.arrQuestion = list;
		this.flag = flag;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		/*
		 * position: la vi tri cua bien bao trong list convertView: dung de lay ve
		 * cac control cua moi item parent: chinh la datasource duoc truyen
		 * vao tu DocActivity
		 */

		if (convertView == null) {
			LayoutInflater inflater = context.getLayoutInflater();
			convertView = inflater.inflate(layoutId, null);
		}

		// Lay ve vi tri duoc yeu cau
		DocPojo doc = arrQuestion.get(position);

		// Lay ra nhung control duoc dinh nghia trong cau truc cua moi item
		if (flag == 1) { //thuoc ve phan cau hoi ly thuyet tabtheory
			TextView ask = (TextView) convertView
					.findViewById(R.id.tvAskTheory);
			TextView ans1 = (TextView) convertView
					.findViewById(R.id.tvAns1Theory);
			TextView ans2 = (TextView) convertView
					.findViewById(R.id.tvAns2Theory);
			TextView ans3 = (TextView) convertView
					.findViewById(R.id.tvAns3Theory);
			TextView ans4 = (TextView) convertView
					.findViewById(R.id.tvAns4Theory);
			TextView trueans = (TextView) convertView
					.findViewById(R.id.tvTrueAnsTheory);

			// Gan gia tri control do
			ask.setText(doc.getAsk());
			ans1.setText(doc.getAns1());
			ans2.setText(doc.getAns2());
			ans3.setText(doc.getAns3());
			ans4.setText(doc.getAns4());
			trueans.setText("Đáp án: " + doc.getTrueans());
		} else if (flag == 2) { //thuoc ve phan cau hoi bien bao tabsigns
			TextView ask = (TextView) convertView.findViewById(R.id.tvAskSigns);
			ImageView img = (ImageView) convertView
					.findViewById(R.id.ivImgSigns);
			TextView ans1 = (TextView) convertView
					.findViewById(R.id.tvAns1Signs);
			TextView ans2 = (TextView) convertView
					.findViewById(R.id.tvAns2Signs);
			TextView ans3 = (TextView) convertView
					.findViewById(R.id.tvAns3Signs);
			TextView ans4 = (TextView) convertView
					.findViewById(R.id.tvAns4Signs);
			TextView trueans = (TextView) convertView
					.findViewById(R.id.tvTrueAnsSigns);

			// Gan gia tri cho control do
			ask.setText(doc.getAsk());
			String uri_img = "drawable/" + doc.getImg();
			int ImageResource = convertView
					.getContext()
					.getResources()
					.getIdentifier(
							uri_img,
							null,
							convertView.getContext().getApplicationContext()
									.getPackageName());
			Drawable image = convertView.getContext().getResources()
					.getDrawable(ImageResource);
			img.setImageDrawable(image);
			ans1.setText(doc.getAns1());
			ans2.setText(doc.getAns2());
			ans3.setText(doc.getAns3());
			ans4.setText(doc.getAns4());
			trueans.setText("Đáp án: " + doc.getTrueans());
		} else if (flag == 3) { //thuoc ve phan cau hoi tinh huong tabcircumstance
			TextView ask = (TextView) convertView.findViewById(R.id.tvAskCir);
			ImageView img = (ImageView) convertView.findViewById(R.id.ivImgCir);
			TextView ans1 = (TextView) convertView.findViewById(R.id.tvAns1Cir);
			TextView ans2 = (TextView) convertView.findViewById(R.id.tvAns2Cir);
			TextView ans3 = (TextView) convertView.findViewById(R.id.tvAns3Cir);
			TextView ans4 = (TextView) convertView.findViewById(R.id.tvAns4Cir);
			TextView trueans = (TextView) convertView
					.findViewById(R.id.tvTrueAnsCir);

			// Gan gia tri cho control do
			ask.setText(doc.getAsk());
			String uri_img = "drawable/" + doc.getImg();
			int ImageResource = convertView
					.getContext()
					.getResources()
					.getIdentifier(
							uri_img,
							null,
							convertView.getContext().getApplicationContext()
									.getPackageName());
			Drawable image = convertView.getContext().getResources()
					.getDrawable(ImageResource);
			img.setImageDrawable(image);
			ans1.setText(doc.getAns1());
			ans2.setText(doc.getAns2());
			ans3.setText(doc.getAns3());
			ans4.setText(doc.getAns4());
			trueans.setText("Đáp án: " + doc.getTrueans());
		}

		return convertView;
	}
}
